'''This module provides the client class, resource definitions, and related key
and mouse enumerations for input.
'''
from __future__ import print_function
from __future__ import unicode_literals
from __future__ import division

import logging

from kharlia.core import NativeClass, LoadConfig, CObject, CEvent

from kharlia.engine.base import GetEngine

from kharlia.__engine import CClientViewport
from kharlia._engine import (FResource, FTexture, CSpriteAniDefBase, CClientBase,
                             CUIScriptModuleBase, EKey, EMouseButton)

__all__ = [
    'EKey',
    'EMouseButton',
    'FResource',
    'FTexture',
    'CClientViewport',
    'CClient',
    'CUIScriptModule',
    'CSpriteAniDef'
    ]

GLogger = logging.getLogger('engine')

@NativeClass
class CClient(CClientBase, CObject):
    def __init__(self):
        self.__initnative__()
        CObject.__init__(self)
        self.EventUIExiting = CEvent()
        self.EventPreRender = CEvent()
        
    def _OnUIExiting(self):
        '''Called by base when CEGUI is exiting.'''
        self.EventUIExiting(self)

    def _OnPreRender(self):
        '''Called by base at the beginning of DrawViewport().'''
        self.EventPreRender(self)

    def OnViewportActivate(self, viewport, active):
        '''Called by the wx viewport during an activate event.'''
        self.Engine.OnViewportActivate(viewport, active)

@NativeClass
class CUIScriptModule(CUIScriptModuleBase, CObject):
    def __init__(self):
        self.__initnative__()
        self.Subscribers = {}

    def OnCreateBindings(self):
        pass

    def OnDestroyBindings(self):
        pass

    def OnAddSubscriber(self, callable):
        pass

    def GetBoundCallable(self, name):
        return self.Subscribers.get(name, None)

@NativeClass
class CSpriteAniDef(CSpriteAniDefBase, CObject):
    def __init__(self, name):
        self.__initnative__(None, name)

    def LoadDefFile(self, name):
        rpath = GetEngine().ResolvePath(name)
        if not rpath:
            raise IOError('resource file does not exist: {}'.foramt(name))
        return LoadConfig(rpath)

    def IterFrames(self, listindex):
        get = self.GetFrame
        for i in xrange(self.GetFrameCount(listindex)):
            yield get(listindex, i)

    def IterFrameLists(self):
        get = self.IterFrames
        for i in xrange(self.FrameListCount):
            yield tuple(get(i))
